package com.stox.indicator;

import java.util.List;

import org.springframework.stereotype.Component;

import com.stox.dto.Bar;
import com.stox.dto.BarValue;

@Component
public class SimpleMovingAverage extends AbstractMovingAverage{
		
	@Override
	public Double getValue(Configuration configuration, List<Bar> bars, int index){
		double sum = 0;
		int span = configuration.getSpan();
		BarValue barValue = configuration.getBarValue();
		for(int i = index; i < index + span; i++){
			sum += barValue.get(bars.get(i));
		}
		return sum/span;
	}
	
	public Double[] getValues(Configuration configuration, Double[] values){
		Double[] result = new Double[values.length];
		double sum = 0;
		int span = configuration.getSpan();
		int size = values.length;
		for(int i = size - 1; i >= 0; i--){
			if(null == values[i]) continue;
			sum += values[i];
			if(i > size - 1 - span) continue;
			if(null == values[i + span]) continue;
			sum -= values[i + span];
			result[i] = sum/span;
		}
		return result;
	}
	
	@Override
	public Double[] getValues(Configuration configuration, List<Bar> bars) {
		Double[] result = new Double[bars.size()];
		double sum = 0;
		int span = configuration.getSpan();
		BarValue barValue = configuration.getBarValue();
		int size = bars.size();
		for(int i = size - 1; i >= 0; i--){
			sum += barValue.get(bars.get(i));
			if(i > size - 1 - span) continue;
			sum -= barValue.get(bars.get(i + span));
			result[i] = sum/span;
		}
		return result;
	}
}
